<?php

namespace Org\Tbk;

include "top/ResultSet.php";

include "top/TopClient.php";

include "top/TopLogger.php";

include "top/RequestCheckUtil.php";

include "top/request/TbkItemGetRequest.php";

include "top/request/TbkUatmFavoritesGetRequest.php";

include "top/request/TbkItemRecommendGetRequest.php";

include "top/request/TbkItemInfoGetRequest.php";

include "top/request/TbkShopGetRequest.php";

include "top/request/TbkShopRecommendGetRequest.php";

include "top/request/TbkUatmEventGetRequest.php";

include "top/request/TbkJuTqgGetRequest.php";

include "top/request/TbkUatmFavoritesItemGetRequest.php";

class Tbk
{

    //返回提取的数据

    public function toClient($req)
    {

        $conf = C('TBK');

        $c = new TopClient;

        $c->appkey = $conf['appkey'];

        $c->secretKey = $conf['appsecret'];

        $c->format = "json";

        $res = $c->execute($req);

        return json_decode(json_encode($res), true); //转成php数组

    }

    /**  淘宝客商品查询  适用于搜索 及分类查询*/

    public function getGoodsRequest($goodsname, $fields, $limit)
    {

        $Api = M('api');

        $conf = $Api->find(1);

        $req = new TbkItemGetRequest;

        if (!$fields) {

            $fields = 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick';

        }

        if (!$limit) {
            $limit = "{$conf['page_size']}";
        }

        $req->setFields($fields); //过滤数据 必填

        $req->setQ($goodsname); //名称

        $req->setItemloc("{$conf['itemloc']}"); // 城市

        $req->setSort("{$conf['sort']}"); //排序

        $req->setIsTmall("false"); //是否商城商品 false 为不判断

        $req->setIsOverseas("false"); //是否海外商品

        $req->setStartPrice("{$conf['start_price']}"); // 折扣价范围下限

        $req->setEndPrice("{$conf['end_price']}"); //折扣价范围上限

        $req->setStartTkRate("{$conf['start_tk_rate']}"); //淘客佣金比率上限，如：1234表示12.34%

        $req->setEndTkRate("{$conf['end_tk_rate']}"); //淘客佣金比率下限，如：1234表示12.34%

        $req->setPlatform('1'); //链接形式：1：PC，2：无线，默认：１

        $req->setPageNo("{$conf['page_no']}"); //第几页，默认：１

        $req->setPageSize($limit); //页大小，默认20，1~100

        return $this->toClient($req);

    }

    /** 淘宝客商品关联推荐查询 */

    public function getRecommendGetRequest($data)
    {

        $req = new TbkItemRecommendGetRequest;

        $req->setFields($data['field']); //过滤数据，必填

        $req->setNumIid($data['numid']); //商品Id

        $req->setCount($data['count']); //返回数量，默认20，最大值40

        $req->setPlatform($data['platform']); // 链接形式：1：PC，2：无线，默认：１

        return $this->toClient($req);

    }

    /** 淘宝客商品详情（简版）*/

    public function getItemInfoGetRequest($data)
    {

        $req = new TbkItemInfoGetRequest;

        $req->setFields($data['field']);

        $req->setPlatform($data['platform']);

        $req->setNumIids($data['numid']);

        return $this->toClient($req);

    }

    /** 淘抢购a */

    public function getJuTqgGetRequest($data, $fields)
    {

        if (!$fields) {

            $fields = 'click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time';

        }

        $Api = M('api');

        $conf = $Api->find(2);

        $req = new TbkJuTqgGetRequest;

        $req->setAdzoneId("{$conf['adzoneid']}");

        $req->setFields($fields);

        $req->setStartTime("{$conf['start_time']}");

        $req->setEndTime("{$conf['end_time']}");

        $req->setPageNo("{$conf['page_no']}");

        $req->setPageSize("{$conf['page_size']}");

        return $this->toClient($req);

    }

    /**接选品库， 得到选品 id*/

    public function getUatmFavoritesGetRequest()
    {

        $req = new TbkUatmFavoritesGetRequest;

        $req->setPageNo("1");

        $req->setPageSize("20");

        $req->setFields("favorites_title,favorites_id,type");

        $req->setType("1");

        return $this->toClient($req);

    }

    /** 由 选品id 获得列表 可用*/

    public function getUatmFavoritesItemGetRequest($id, $fields)
    {

        if (!$fields) {

            $fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type";

        }

        $Api = M('api');

        $conf = $Api->find(3);

        $req = new TbkUatmFavoritesItemGetRequest;

        $req->setPlatform("1");

        $req->setPageSize("{$conf['page_size']}");

        $req->setAdzoneId("{$conf['adzoneid']}");

        $req->setFavoritesId("$id");

        $req->setPageNo("{$conf['page_no']}");

        $req->setFields($fields);

        return $this->toClient($req);

    }

    public function getItemCouponGetRequest($goodsname)
    {

        $c = new TopClient;

        $req = new TbkItemCouponGetRequest;

        $req->setPlatform("1");

        $req->setPageSize("48");

        $req->setQ($goodsname);

        $req->setPageNo("1");

        $req->setPid("mm_13772871_22072891_73800373");

        $resp = $c->execute($req);

    }

}
